package com.example.myapplication.found;

import com.example.myapplication.found.bean.FoundDayPointBean;
import com.example.myapplication.found.bean.MinPointBean;
import com.example.myapplication.found.file.FoundDayJsonFile;
import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.Month;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2021/9/14
 * @description
 */
public class Lsjz {
    public static boolean isTest = false;
    List<FoundDayPointBean> foundDayPointBeanList;

    public float[] mcValue;
    public int[] mDays;

    private String fundCode = "161121";
    private String fundName = "银行etf";

    public interface FenxiCallback {
        void callback(Attention attention);
    }

    public interface Fenxi2Callback {
        void callback(List<List<Float>> list);
    }

    FenxiCallback callback;
    Fenxi2Callback callback2;
    ExecutorService pool;
    FoundDayJsonFile foundDayJsonFile;

    public void init() {
        pool = Executors.newFixedThreadPool(5);
        float[] cValue = new float[10];
        float frist = 0.5f;
        for (int i = 0; i < cValue.length; i++) {
            DecimalFormat df2 = new DecimalFormat("0.0");
            frist = Float.parseFloat(df2.format(frist));
            cValue[i] = frist;
            frist = (frist + 0.5f);
        }
        mcValue = cValue;

        int[] m = new int[24];
        for (int i = 0; i < m.length; i++) {
            m[i] = i + 21;
        }
        mDays = m;
        foundDayJsonFile = new FoundDayJsonFile();
    }

    public Lsjz(String fundCode, String fundName) {
        this.fundCode = fundCode;
        this.fundName = fundName;
        init();
    }


    public void setCallback(FenxiCallback callback) {
        this.callback = callback;
    }

    public void setCallback2(Fenxi2Callback callback) {
        this.callback2 = callback;
    }

    public static void main(String[] args) throws Exception {
        Lsjz lsjz = new Lsjz("161121", "银行etf");
        lsjz.start();
    }

    public void start() {
        if ((foundDayPointBeanList = foundDayJsonFile.getSave(fundCode)) != null) {
            fenxi();
        } else {
            foundDayPointBeanList = new ArrayList<>();
            getDns("http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18308966530657147356_1631621533051&fundCode=" + fundCode + "&pageIndex=1&pageSize=550&startDate=&endDate=&_=1631621533069");
        }
    }


    void fenxi() {
        Attention attention = null;
        if (foundDayPointBeanList.size() > 250) {
//            Bean minBean = fenxi4();
//            attention = fenxi5(minBean, list.get(0).dwjz);
            fenxi6();
        }

        if (callback != null) {
            callback.callback(attention);
        }
        if (callback2 != null) {
            callback2.callback(averagelist);
        }
    }

    //找最低点
    List<MinPointBean> getMinPointList() {
        List<MinPointBean> minPointBeanList = new ArrayList<>();
        for (int i = 0; i < foundDayPointBeanList.size(); i++) {
            FoundDayPointBean item = foundDayPointBeanList.get(i);
            if (minPointBeanList.size() < mDays.length) {
                minPointBeanList.add(new MinPointBean(mDays[i], item));
            } else {
                for (int j = 0; j < minPointBeanList.size(); j++) {
                    MinPointBean minPointBean = minPointBeanList.get(j);
                    if (item.index < minPointBean.day) {
                        if (item.dwjz < minPointBean.dayPointBean.dwjz) {
                            minPointBean.dayPointBean = item;
                        }
                    }
                }
            }
        }

        if (isTest) {
            for (MinPointBean minPointBean : minPointBeanList) {
                System.out.println(minPointBean.day + "天最低点 " + minPointBean.dayPointBean.dwjz + " " + minPointBean.dayPointBean.FSRQ);
            }
        }
        return minPointBeanList;
    }


    //找最佳买点
    void model1() {
        pool.execute(new Runnable() {
            @Override
            public synchronized void run() {


            }
        });
    }


    List<List<Float>> averagelist = new ArrayList();

    //测试历史数据
    void fenxi6() {
        for (int i = 0; i < foundDayPointBeanList.size(); i++) {
            FoundDayPointBean item = foundDayPointBeanList.get(i);
            List<MinPointBean> bean = getMinPointList();


            for (MinPointBean minBean : bean) {
                if (item.index < minBean.day - 7) {
                    Attention attention = fenxi5(minBean.dayPointBean, item.dwjz);
                    if (attention.bean != null) {
//                        System.out.println(fundName + " " + fundCode);
//                        System.out.println("支撑位 " + attention.bean.FSRQ + " " + attention.bean.dwjz);
//                        for (String des : attention.des) {
//                            System.out.println(des);
//                        }
//                        System.out.println(item.FSRQ + " 之后3天涨跌幅 ");
                        float average = 0;
                        List<Float> averageList = new ArrayList<>();
                        for (int j = 1; j < 6; j++) {
                            if ((i - j) < foundDayPointBeanList.size() && (i - j) > 0) {
                                FoundDayPointBean nextItem = foundDayPointBeanList.get(i - j);
                                average += nextItem.jzjz;
                                averageList.add(nextItem.jzjz);
//                                System.out.println(nextItem.FSRQ + " " + nextItem.jzjz + "%");
                            }
                        }
                        averagelist.add(averageList);
                        i = i + 21;
                    }
                }
            }
        }
    }


    public boolean isAttention;

    //找支撑位
    Attention fenxi5(FoundDayPointBean minBean, float todayDwjz) {
        Attention attention = new Attention();
        if (isTest) {
            System.out.println("昨日点位 " + todayDwjz);
        }

        DecimalFormat decimalFormat = new DecimalFormat("0.0000");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        String p1 = decimalFormat.format(minBean.dwjz - todayDwjz);
        float cValue = (todayDwjz - minBean.dwjz) / minBean.dwjz * 100f;
        String p2 = decimalFormat.format(cValue);
//                System.out.println(month[i] + "个月误差 " + p1 + " " + p2 + "%");

//        if (Math.abs(cValue) <= mcValue) {
//            isAttention = true;
//            attention.bean = minBean;
//            attention.fundCode = fundCode;
//            attention.fundName = fundName;
//            attention.des.add(minBean.month + "个月误差 " + p1 + " " + p2 + "%");
//        }

        return attention;
    }

    static class Attention {
        public String fundName;
        public String fundCode;
        public FoundDayPointBean bean;
        public List<String> des = new ArrayList<>();
    }

    static class Lishi {
        public String fundName;
        public String fundCode;
        List<Float> averagelist = new ArrayList();
    }


    static class NearBean {
        int month;
        float nearLastDwjz = 1;
        FoundDayPointBean nearLastBean = null;
        int nearLastIndex = 0;

        public NearBean(int month) {
            this.month = month;
        }
    }

    void fenxi2() {
        Collections.reverse(foundDayPointBeanList);
        int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0, count7 = 0, count8 = 0, count9 = 0, count10 = 0;
        int reduce0 = 0, reduce1 = 0, reduce2 = 0, reduce3 = 0, reduce4 = 0, reduce5 = 0, reduce6 = 0, reduce7 = 0, reduce8 = 0, reduce9 = 0, reduce10 = 0;
        for (int i = 1; i < foundDayPointBeanList.size(); i++) {
            FoundDayPointBean item = foundDayPointBeanList.get(i);

            if (item.jzjz < -0.10) {
                reduce10++;
            } else if (item.jzjz >= -0.10 && item.jzjz < -0.09) {
                reduce9++;
            } else if (item.jzjz >= -0.09 && item.jzjz < -0.08) {
                reduce8++;
            } else if (item.jzjz >= -0.08 && item.jzjz < -0.07) {
                reduce7++;
            } else if (item.jzjz >= -0.07 && item.jzjz < -0.06) {
                reduce6++;
            } else if (item.jzjz >= -0.06 && item.jzjz < -0.05) {
                reduce5++;
            } else if (item.jzjz >= -0.05 && item.jzjz < -0.04) {
                reduce4++;
            } else if (item.jzjz >= -0.04 && item.jzjz < -0.03) {
                reduce3++;
            } else if (item.jzjz >= -0.03 && item.jzjz < -0.02) {
                reduce2++;
            } else if (item.jzjz >= -0.02 && item.jzjz < -0.01) {
                reduce1++;
            } else if (item.jzjz >= -0.01 && item.jzjz < 0.00) {
                reduce0++;
            } else if (item.jzjz >= 0 && item.jzjz < 0.01) {
                count0++;
            } else if (item.jzjz >= 0.01 && item.jzjz < 0.02) {
                count1++;
            } else if (item.jzjz >= 0.02 && item.jzjz < 0.03) {
                count2++;
            } else if (item.jzjz >= 0.03 && item.jzjz < 0.04) {
                count3++;
            } else if (item.jzjz >= 0.04 && item.jzjz < 0.05) {
                count4++;
            } else if (item.jzjz >= 0.05 && item.jzjz < 0.06) {
                count5++;
            } else if (item.jzjz >= 0.06 && item.jzjz < 0.07) {
                count6++;
            } else if (item.jzjz >= 0.07 && item.jzjz < 0.08) {
                count7++;
            } else if (item.jzjz >= 0.08 && item.jzjz < 0.09) {
                count8++;
            } else if (item.jzjz >= 0.09 && item.jzjz < 0.10) {
                count9++;
            } else if (item.jzjz >= 0.10) {
                count10++;
            }
        }
        System.out.println("<-10% " + reduce10);
        System.out.println("-10%~-9% " + reduce9);
        System.out.println("-9%~-8% " + reduce8);
        System.out.println("-8%~-7% " + reduce7);
        System.out.println("-7%~-6% " + reduce6);
        System.out.println("-6%~-5% " + reduce5);
        System.out.println("-5%~-4% " + reduce4);
        System.out.println("-4%~-3% " + reduce3);
        System.out.println("-3%~-2% " + reduce2);
        System.out.println("-2%~-1% " + reduce1);
        System.out.println("-1%~-0% " + reduce0);
        System.out.println("0%~1% " + count0);
        System.out.println("1%~2% " + count1);
        System.out.println("2%~3% " + count2);
        System.out.println("3%~4% " + count3);
        System.out.println("4%~5% " + count4);
        System.out.println("5%~6% " + count5);
        System.out.println("6%~7% " + count6);
        System.out.println("7%~8% " + count7);
        System.out.println("8%~9% " + count8);
        System.out.println("9%~10% " + count9);
        System.out.println(">10% " + count10);
    }

    public void getDns(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("Referer", "http://fundf10.eastmoney.com/")
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    html = html.replace("jQuery18308966530657147356_1631621533051({\"Data\":{\"LSJZList\":", "");
                    int index = html.indexOf("]");
                    html = html.substring(0, index + 1);
                    List<FoundDayPointBean> bean = GsonUtil.parse(html, new TypeToken<List<FoundDayPointBean>>() {
                    }.getType());
                    int listIndex = 0;
                    for (FoundDayPointBean item : bean) {
                        try {
                            if (item != null) {
                                item.jzjz = Float.parseFloat(item.JZZZL);
                                item.dwjz = Float.parseFloat(item.DWJZ);
                                item.index = listIndex;
                                foundDayPointBeanList.add(item);
                                listIndex++;
                            }
                        } catch (NumberFormatException e) {
                        }
                    }
                    foundDayJsonFile.save(foundDayPointBeanList, fundCode);
                    fenxi();
                }
            }
        });
    }


}
